<?php

namespace app\admin\model\choi;

use think\Model;
use traits\model\SoftDelete;

class Course extends Model
{

    use SoftDelete;

    

    // 表名
    protected $table = 'course';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = 'deletetime';

    // 追加属性
    protected $append = [
        'time_text',
        'status_text'
    ];
    

    
    public function getStatusList()
    {
        return ['0' => __('Status 0'), '1' => __('Status 1'), '2' => __('Status 2'), '3' => __('Status 3'), '4' => __('Status 4'), '5' => __('Status 5'), '6' => __('Status 6'), '7' => __('Status 7')];
    }

    public function getStatus()
    {
        return ['2' => __('Status 2'), '3' => __('Status 3'), '4' => __('Status 4')];
    }

    public function getEditStatusList()
    {
        return ['3' => __('Status 3'), '4' => __('Status 4')];
    }

    public function getTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['time']) ? $data['time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }

//    protected function setTimeAttr($value)
//    {
//        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
//    }


    public function teacher()
    {
        return $this->hasOne(Teacher::class, 'teacher_id', 'teacher_id');
    }

    public function user()
    {
        return $this->hasOne(User::class, 'user_id', 'user_id');
    }

    public function courseOrder()
    {
        return $this->hasOne(CourseOrder::class, 'course_order_id', 'course_order_id');
    }

    /**
     * 更新状态
     */
    public static function updateStatus($courseOrderId)
    {
        return self::where('course_order_id', $courseOrderId)->where('status', 5)->update(['status' => 7]);
    }

    /**
     * 回退课表状态
     */
    public static function rollbackStatus($courseOrderId)
    {
        $courses = self::getByCourseOrderIdAndStatus($courseOrderId, [5]);
        if ($courses) {
            foreach ($courses as &$val) {
                if ($val['date'] && $val['time']) {
                    $course = self::where('course_id', '<>', $val['course_id'])->where('date', $val['date'])->where('time', $val['time'])->find();
                    $times = explode('-', $val['time']);
                    if ($course || time() > strtotime($val['date'].' '.$times[0])) {
                        $val['date'] = '';
                        $val['time'] = '';
                        $val['week'] = '';
                        $val['status'] = 2;
                    } else {
                        $val->status = 3;
                    }
                } else {
                    $val->status = 2;
                }
                $val->save();
            }
        }
    }

    /**
     * 获取课表
     */
    public static function getByCourseOrderIdAndStatus($courseOrderId, $status)
    {
        return self::where('course_order_id', $courseOrderId)->whereIn('status', $status)->select();
    }



}
